/**
 * Course			:	ISYS2119 - BIS Development 2
 * Assignment		:	03
 * Date due			:	31/08/2012
 * Program 			:	Saigon Bike Rental Catalogue System
 * 
 * Programmer		:	Vuu Ngoc My Linh - s3342015, Tran Thi Van Anh - s3342025
 * 
 * Attributions		:	Lecture slides, Blackboard, Tutorials, Websites (not in reference list document)
 * 
 * Function list	: 	public static void main(String[] args) * 						
 */

package rmit.ISYS2119.assign03;

import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.io.*;

public class BikeManager {

	public static void main(String[] args) {

		// Create GUI
		FormBikeCatalogue form = new FormBikeCatalogue();
		form.setVisible(true);

		// Declare variable
		int bikeId;
		int year;
		boolean validateName;
		boolean validateYear;

		try {
			Bike bike = new Bike();
			Scanner scanner = new Scanner(System.in);

			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));

			// Welcome statement
			System.out.println("Welcome Bike Manager");
			System.out.println("********************");
			System.out.print("\n");

			// Input number of bikes
			System.out.print("Please enter the number of bikes: ");

			// Number of bikes must be a number
			while (!scanner.hasNextInt()) {
				System.out.println("INVALID! It must be a number.");
				System.out.print("Please re-enter the number of bikes: ");
				scanner.next();
			}
			int numberOfBikes = scanner.nextInt();

			// Number of bikes must be positive
			while (Validation.validateNumberOfBikes(numberOfBikes) == false) {
				System.out.println("INVALID! Number of bikes must be > 0.");
				System.out.print("Please re-enter the number of bikes: ");
				numberOfBikes = scanner.nextInt();
			}

			int[] bikeIdList;
			bikeIdList = new int[numberOfBikes];

			for (int i = 0; i < numberOfBikes; i++) {
				System.out.print("\n");
				System.out.println("Please enter details of bike " + (i + 1)
						+ ": ");
				System.out.println("******************************");
				boolean unique = false;

				// Input Bike ID
				do {
					System.out.print("Bike ID: ");

					// Bike ID must be a number
					while (!scanner.hasNextInt()) {
						System.out
								.print("Bike ID has to be a number. Please re-enter the Bike ID: ");
						scanner.next();
					}
					bikeId = scanner.nextInt();

					// Call a method to validate that bike id is in range of
					// 1-9999
					// ======================================
					while (Validation.validateBikeID(bikeId) == false) {
						System.out
								.println("INVALID! Please enter bike id in range of [0001-9999]");
						System.out.print("Bike ID: ");
						bikeId = scanner.nextInt();
					}

					// Bike ID must be unique
					if (Validation.notInBikeIdList(bikeId, bikeIdList)) {
						bikeIdList[i] = bikeId;
						unique = true;
						bike.setBikeId(bikeId);
					} else {
						System.out
								.println("Bike ID has to be unique. Please re-enter the Bike ID.");
					}

				} while (!unique);

				// Input Model Name
				String name = null;
				validateName = false;
				do {
					System.out.print("Name: ");
					name = br.readLine();

					// Validate name
					if (Validation.isNameValid(name)) {
						validateName = true;
						bike.setModelName(name);
					} else {
						System.out
								.println("Invalid value for attribute 'Model name'.  Valid value must only contain alphabetic characters and space between them. Please re-enter the Model Name.");
					}

				} while (!validateName);

				// Input year
				do {
					System.out.print("Year: ");
					validateYear = false;

					// Year must be a number
					while (!scanner.hasNextInt()) {
						System.out
								.print("Year has to be a number. Please re-enter the year: ");
						scanner.next();
					}
					year = scanner.nextInt();

					// Validate year
					if (Validation.isYearValid(year)) {
						validateYear = true;
						bike.setYear(year);
					} else {
						System.out
								.println("Invalid value for attribute 'Year'.  Valid value must be from 2000 to current year. Please re-enter the Year.");
					}

				} while (!validateYear);

				// Input Description
				System.out.print("Description: ");
				String description = br.readLine();
				bike.setDescription(description);

				// Display output
				System.out.print("\n");
				System.out.println("Details of bike " + (i + 1) + ": ");
				System.out.println("*****************");
				System.out.print("\n");
				System.out.println("+ Bike ID: " + bike.getBikeId());
				System.out.println("+ Name: " + bike.getModelName());
				System.out.println("+ Year: " + bike.getYear());
				System.out.println("+ Description: " + bike.getDescription());

				// Determine the Engine Capacity
				bike.setEngineCapacity(bike.getModelName());
				System.out.println("+ Engine Capacity: "
						+ bike.getEngineCapacity() + " (cc)");

				// Determine the Monthly Rent
				bike.setRentalCost(year, bike.getEngineCapacity(),
						bike.getModelName());

				// Format numbers
				NumberFormat formatter = new DecimalFormat("#0,000,000");
				System.out.println("+ Monthly Rental: "
						+ formatter.format(bike.getRentalCost()) + " (VND)");
			}

			// If error occurs
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
			e.printStackTrace();
		}
	}

}
